﻿using IRealize;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Entity;
using Microsoft.Data.SqlClient;
using System.Threading.Channels;
using Dapper;

namespace Realize
{
    public class Config_Major : IConfig_Major
    {
        public async Task<List<Entity.Config_Major>> select_major_name(string sqlserver)
        {
            Task<List<Entity.Config_Major>> task = Task<List<Entity.Config_Major>>.Run(async () =>
            {
                using (SqlConnection con = new SqlConnection(sqlserver))
                {
                    string sql = "select * from config_major";
                    return con.Query<Entity.Config_Major>(sql).ToList();
                }
            });
            return await task;
        }

        public async Task<List<Entity.Config_Major>> selectById(string id,string sqlserver)
        {
            Task<List<Entity.Config_Major>> task = Task<List<Entity.Config_Major>>.Run(async () =>
            {
                using (SqlConnection con = new SqlConnection(sqlserver))
                {
                    string sql = $@"select * from config_major where major_kind_id={id}";
                    return con.Query<Entity.Config_Major>(sql).ToList();
                }
            });
            return await task;
        }

        public async Task<int> delete(string id, string sqlserver)
        {
            using (SqlConnection con = new SqlConnection(sqlserver))
            {
                string sql = $"delete from config_major where mak_id = {id}";
                return await con.ExecuteAsync(sql);
            }
        }

        public async Task<int> Add(string id1, string id2, string name1, string name2, string sqlserver)
        {
            using (SqlConnection con = new SqlConnection(sqlserver))
            {
                //string sql = $@"insert into config_major(major_kind_id,major_kind_name,major_id,major_name) 
                //                values('{cm.major_kind_id}','{cm.major_kind_name}','{cm.major_id}','{cm.major_name}')";

                string sql = $@"insert into config_major(major_kind_id,major_kind_name,major_id,major_name) 
                                values('{id1}','{name1}','{id2}','{name2}')";

                return await con.ExecuteAsync(sql);
            }
        }
    }
}
